/*
模块OTDR仪表
仅仅设置部分回调函数

2021.7.28 by Menglong Wu
*/
#include "port.h"
#include "fpga-common.h"
#include "fpga-private.h"
#include "interface/protocal-otdr.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/*
 * 1 点亮LED；0关闭LED，另外还有hook shell
 */
static void _do_otdr_test_led(int on_off)
{
    char  strcmd[256];
    FILE *fp;

    snprintf(strcmd,
             sizeof(strcmd),
             "/sys/class/gpio/%s/value",
             env_dev_led_run);
    fp = fopen(strcmd, "w");
    if (fp) {

	fprintf(fp, "%d", on_off);
	fclose(fp);
    }
    else {
	// printf("没有IO\r\n");
    }

    // 其他shell
    switch (on_off) {
    case 1:
	snprintf(strcmd, sizeof(strcmd), "/app/do_otdr_test_start.sh");
	break;
    case 0:
	snprintf(strcmd, sizeof(strcmd), "/app/do_otdr_test_finish.sh");
	break;
    }

    if ((access(strcmd, F_OK)) != -1) {
	// printf("文件存在\r\n");
	int ret = system(strcmd);
    }
    else {
	// printf("no文件存在\r\n");
    }
}


// int set_gpio(int io, int val)
static int set_gpio(struct fpga_dev *dev, int io, int val)
{
    // 只有一个LED，而且val只能传递0、1，这个在驱动里控制，不去判断val值了
    _do_otdr_test_led(val);
    return 0;
}

static struct priv_otdr_data data_m_32k = {
    .cache_len   = 64000 * 4 + 4000,
    .sample_byte = 64 * 1000 * 4,
};

static struct fpga_if otdr_interface_m_32k = {
    DEFAULT_FPGA_IF,
    .pfun_probe      = cb_fpga_otdr_probe,
    .pfun_release    = cb_fpga_otdr_release,
    .private_data    = &data_m_32k,
    .name            = "this-otdr-m-32k",
    .pfun_start      = cb_fpga_otdr_start,
    .pfun_isfinish   = cb_fpga_otdr_isfinish,
    .pfun_stop       = cb_fpga_otdr_stop,
    .pfun_set_gpio   = set_gpio,       // TODO 控制LED
    .pfun_get_gpio   = none_get_gpio,  // TODO 控制LED
    .pfun_version    = com_fpga_version,
    .pfun_read_frame = cb_fpga_read_otdr_data,
    .pfun_write_regs = cb_fpga_otdr_write_regs,
};
static void __attribute__((constructor(INIT_BSP))) this_init()
{
    fpga_if_register(&otdr_interface_m_32k);
}